package cz.jm.web.jmlib.client.dialog.registration;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

import cz.jm.web.jmlib.client.dialog.DialogWidget;
import cz.jm.web.jmlib.client.dialog.items.DialogItem;
import cz.jm.web.jmlib.client.dialog.items.InputBox;
import cz.jm.web.jmlib.client.dialog.items.PasswordBox;
import cz.jm.web.jmlib.client.dialog.registration.RegistrationPresenter.Display;
import cz.jm.web.jmlib.client.dialog.validation.validators.MailValidator;
import cz.jm.web.jmlib.client.dialog.validation.validators.PasswordValidator;
import cz.jm.web.jmlib.client.dialog.validation.validators.RegexpValidator;
import cz.jm.web.jmlib.client.dialog.validation.validators.RequiredValueValidator;
import cz.jm.web.jmlib.client.dialog.validation.validators.ValueVerificationValidator;
import cz.jm.web.jmlib.client.resources.Resources;

public class RegistrationView implements Display {
	protected final RegistrationConstants constants = GWT.create(RegistrationConstants.class);
	
	private final InputBox usernameInput = new InputBox();
	private final PasswordBox passwordInput = new PasswordBox();
	private final PasswordBox passwordVerificationInput = new PasswordBox();
	private final InputBox mailInput = new InputBox();
	private final InputBox mailVerificationInput = new InputBox();
	
	private final DialogWidget dialog = new DialogWidget();
	
	public RegistrationView() {
		
		dialog.addOkButton();

		addDialogItem(usernameInput, constants.username());
		addDialogItem(passwordInput, constants.password());
		addDialogItem(passwordVerificationInput, constants.passwordVerification());
		addDialogItem(mailInput, constants.mail());
		addDialogItem(mailVerificationInput, constants.mailVerification());
		
		passwordInput.addValidator(new PasswordValidator(passwordInput));
		passwordVerificationInput.addValidator(new ValueVerificationValidator(passwordInput, passwordVerificationInput));
		
		mailInput.addValidator(new MailValidator(mailInput));
		mailVerificationInput.addValidator(new ValueVerificationValidator(mailInput, mailVerificationInput));
		
		dialog.setIcon(new Image(Resources.INSTANCE.getRegister()));
		
	}
	
	
	private void addDialogItem(DialogItem item, String txt) {
		item.getLabel().setText(txt);
		item.addValidator(RequiredValueValidator.createValidator(item));		
		dialog.addItem(item);
	}
	
	@Override
    public Widget asWidget() {		
	    return dialog;
    }


	@Override
    public String getUsername() {
	    return usernameInput.getInput().getValue();
    }

	@Override
    public String getPassword() {
	    return passwordInput.getPassword().getValue();
    }

	@Override
    public String getMail() {
	    return mailInput.getInput().getValue();
    }

	@Override
    public HasClickHandlers getOk() {
	    return dialog.getButton();
    }


	@Override
    public boolean isValid() {
	    return dialog.isValid();
    }

}
